package com.navi.loaddata.utils;

import com.navi.loaddata.core.mysqlEntity.QrtzEtlDetail;
import com.navi.loaddata.core.repository.impl.MySqlRepositoryImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SaveEtlPoint {

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private MySqlRepositoryImpl mySqlRepository;

    public void save2etlLayout(LocalDateTime etlStart, List list) {
        LocalDateTime etlEnd = LocalDateTime.now();
        QrtzEtlDetail qrtzEtlDetail = new QrtzEtlDetail();
        qrtzEtlDetail.setProjectName("loaddata");
        qrtzEtlDetail.setInstanceName("mpp_layout_d");
        qrtzEtlDetail.setSchedName("Loadlayout");
        qrtzEtlDetail.setRelFlg("Y");
        qrtzEtlDetail.setCheckinTime(Timestamp.valueOf(etlStart));
        qrtzEtlDetail.setEtlTime(Timestamp.valueOf(etlStart));
        qrtzEtlDetail.setEndTime(Timestamp.valueOf(etlEnd));
        qrtzEtlDetail.setCronExpression("0 0/10 * * * ?");
        qrtzEtlDetail.setCount(list.size());
        mySqlRepository.update(qrtzEtlDetail);
    }

    public void save2etlProduct(LocalDateTime etlStart, List list) {
        LocalDateTime etlEnd = LocalDateTime.now();
        QrtzEtlDetail qrtzEtlDetail = new QrtzEtlDetail();
        qrtzEtlDetail.setProjectName("loaddata");
        qrtzEtlDetail.setInstanceName("wpp_sht_ope_his");
        qrtzEtlDetail.setSchedName("LoadProductHis");
        qrtzEtlDetail.setRelFlg("Y");
        qrtzEtlDetail.setCheckinTime(Timestamp.valueOf(etlStart));
        qrtzEtlDetail.setEtlTime(Timestamp.valueOf(etlStart));
        qrtzEtlDetail.setEndTime(Timestamp.valueOf(etlEnd));
        qrtzEtlDetail.setCronExpression("0 0/5 * * * ?");
        qrtzEtlDetail.setCount(list.size());
        mySqlRepository.update(qrtzEtlDetail);
    }
    public void save2etlProductInfo(LocalDateTime etlStart, List list) {
        LocalDateTime etlEnd = LocalDateTime.now();
        QrtzEtlDetail qrtzEtlDetail = new QrtzEtlDetail();
        qrtzEtlDetail.setProjectName("loaddata");
        qrtzEtlDetail.setInstanceName("wpp_sht_info");
        qrtzEtlDetail.setSchedName("LoadProductWipHis");
        qrtzEtlDetail.setRelFlg("Y");
        qrtzEtlDetail.setCheckinTime(Timestamp.valueOf(etlStart));
        qrtzEtlDetail.setEtlTime(Timestamp.valueOf(etlStart));
        qrtzEtlDetail.setEndTime(Timestamp.valueOf(etlEnd));
        qrtzEtlDetail.setCronExpression("0 0/30 * * * ?");
        qrtzEtlDetail.setCount(list.size());
        mySqlRepository.update(qrtzEtlDetail);
    }

    public void save2etlCT1(Timestamp etlStart, Integer cnt) {
        QrtzEtlDetail qrtzEtlDetail = new QrtzEtlDetail();
        qrtzEtlDetail.setProjectName("loaddata");
        qrtzEtlDetail.setSchedName("LoadCT1His");
        qrtzEtlDetail.setInstanceName("wpp_cdefect_glass_f");
        qrtzEtlDetail.setCheckinTime(etlStart);
        qrtzEtlDetail.setEtlTime(Timestamp.valueOf(dateFormat.format(new Date())));
        qrtzEtlDetail.setRelFlg("Y");
        qrtzEtlDetail.setEndTime(Timestamp.valueOf(dateFormat.format(new Date(etlStart.getTime() + 600000))));
        qrtzEtlDetail.setCronExpression("3000");
        qrtzEtlDetail.setCount(cnt);
        mySqlRepository.update(qrtzEtlDetail);
    }

    public void save2etlSpecialOpe(Timestamp etlStart, Integer cnt) {
        QrtzEtlDetail qrtzEtlDetail = new QrtzEtlDetail();
        qrtzEtlDetail.setProjectName("loaddata");
        qrtzEtlDetail.setSchedName("LoadSpecialOpeYeildData");
        qrtzEtlDetail.setInstanceName("wpp_fdefect_panel_f,wpp_fdefect_defect_f");
        qrtzEtlDetail.setCheckinTime(etlStart);
        qrtzEtlDetail.setEtlTime(Timestamp.valueOf(dateFormat.format(new Date())));
        qrtzEtlDetail.setRelFlg("Y");
        qrtzEtlDetail.setEndTime(Timestamp.valueOf(dateFormat.format(new Date(etlStart.getTime() + 600000))));
        qrtzEtlDetail.setCronExpression("1000");
        qrtzEtlDetail.setCount(cnt);
        mySqlRepository.update(qrtzEtlDetail);
    }
}
